import router from "@/router";
import store from "@/store";
import {getLocalStorage} from "@/utils/localstore";

const whiteRoutePath = ['/user', '/user/login'];

router.beforeEach(async (to, form, next) => {
    const {token, identity} = store.state.login;
    if(token) {
        if(whiteRoutePath.indexOf(to.path) !== -1){
            next({path:'/', replace:true})
        }else {
            if(identity.length === 0 && identity) {
                try {
                    await store.dispatch('login/actionIdentity', getLocalStorage('userName'))
                    const filterChildrenRouter = await store.dispatch('permission/getDefaultMenuList');
                    router.addRoutes(filterChildrenRouter);
                    next({...to, replace: true})
                }catch (e){
                    next({path:'/user/login', replace: true})
                }
            }else {
                next()
            }
        }
    } else {
        if (whiteRoutePath.indexOf(to.path) !== -1) {
            next()
        } else {
            next({path:'/user/login'})
        }
    }
})
